Preprocessing QC statistics

Noam, July 2023

In [1]:
%load_ext autoreload
%autoreload 2
In [2]:
import os
MOMAPS_HOME = '/home/labs/hornsteinlab/Collaboration/MOmaps_Noam/MOmaps'
MOMAPS_DATA_HOME = '/home/labs/hornsteinlab/Collaboration/MOmaps'
LOGS_PATH = os.path.join(MOMAPS_DATA_HOME, 'src', 'preprocessing', 'logs','deltaNLS')
PLOT_PATH = os.path.join(MOMAPS_HOME, 'src', 'preprocessing', 'notebooks','figures','delta_NLS')
os.chdir(MOMAPS_HOME)
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid", font_scale=1.5)
sns.color_palette("husl", 8)
plt.rcParams["image.cmap"] = "Set1"
from tqdm.notebook import tqdm
from src.common.lib.preprocessing_utils import rescale_intensity
from src.common.lib.images_qc import *
#sys.path.insert(1, "/home/labs/hornsteinlab/Collaboration/MOmaps_Sagy/MOmaps/src/common/lib")
import contextlib
import io
import matplotlib
import warnings
warnings.filterwarnings('ignore', category=pd.core.common.SettingWithCopyWarning)
from src.common.lib.qc_config_tmp import *
In [3]:
df = log_files_qc(LOGS_PATH)
Total of 5 files were read.
Before dup handeling  (32524, 20)
After duplication removal #1: (32405, 21)
After duplication removal #2: (32256, 21)

validate folder structure and files existence

In [3]:
# choose batches
batches = [f'batch{i}' for i in range (2,6)]
#batches=['batch5']

Raw Files

In [5]:
root_directory_raw = os.path.join(MOMAPS_DATA_HOME, 'input', 'images', 'raw', 'SpinningDisk','deltaNLS_sort')

batches_raw = [batch.replace("_16bit","") for batch in batches]
raws = run_validate_folder_structure(root_directory_raw, False, dnls_panels, dnls_markers,PLOT_PATH,
                                     dnls_marker_info,dnls_cell_lines_to_cond, reps, dnls_cell_lines_for_disp,
                                     dnls_expected_dapi_raw, batches=batches_raw,fig_width=4, fig_height=10)
batch2
Folder structure is valid.
All files exists.
Total Sites:  27000
========
batch3
Folder structure is invalid. Missing paths:
/home/labs/hornsteinlab/Collaboration/MOmaps/input/images/raw/SpinningDisk/deltaNLS_sort/batch3/WT/panelN
/home/labs/hornsteinlab/Collaboration/MOmaps/input/images/raw/SpinningDisk/deltaNLS_sort/batch3/TDP43/panelN
All files exists.
Total Sites:  25800
========
batch4
Folder structure is invalid. Missing paths:
/home/labs/hornsteinlab/Collaboration/MOmaps/input/images/raw/SpinningDisk/deltaNLS_sort/batch4/WT/panelN
/home/labs/hornsteinlab/Collaboration/MOmaps/input/images/raw/SpinningDisk/deltaNLS_sort/batch4/TDP43/panelN
All files exists.
Total Sites:  25500
========
batch5
Folder structure is valid.
All files exists.
Total Sites:  26897
========
====================

Processed

In [6]:
root_directory_proc = os.path.join(MOMAPS_DATA_HOME, 'input', 'images', 'processed', 'spd2',
                              'SpinningDisk','deltaNLS')
procs = run_validate_folder_structure(root_directory_proc, True, dnls_panels, dnls_markers,PLOT_PATH,
                                     dnls_marker_info,dnls_cell_lines_to_cond, reps, dnls_cell_lines_for_disp,
                                     dnls_expected_dapi_raw, batches=batches_raw, fig_width=4, fig_height=10)
batch2
Folder structure is valid.
Some files are bad:
/home/labs/hornsteinlab/Collaboration/MOmaps/input/images/processed/spd2/SpinningDisk/deltaNLS/batch2/WT/Untreated/TDP43, saved_pngs small size (4.096 kB)
/home/labs/hornsteinlab/Collaboration/MOmaps/input/images/processed/spd2/SpinningDisk/deltaNLS/batch2/WT/Untreated/TDP43, saved_pngs
/home/labs/hornsteinlab/Collaboration/MOmaps/input/images/processed/spd2/SpinningDisk/deltaNLS/batch2/TDP43/dox/TDP43, saved_pngs small size (4.096 kB)
/home/labs/hornsteinlab/Collaboration/MOmaps/input/images/processed/spd2/SpinningDisk/deltaNLS/batch2/TDP43/dox/TDP43, saved_pngs
/home/labs/hornsteinlab/Collaboration/MOmaps/input/images/processed/spd2/SpinningDisk/deltaNLS/batch2/TDP43/Untreated/TDP43, saved_pngs small size (4.096 kB)
/home/labs/hornsteinlab/Collaboration/MOmaps/input/images/processed/spd2/SpinningDisk/deltaNLS/batch2/TDP43/Untreated/TDP43, saved_pngs
Total Sites:  26395
========
batch3
Folder structure is valid.
All files exists.
Total Sites:  25260
========
batch4
Folder structure is valid.
All files exists.
Total Sites:  24827
========
batch5
Folder structure is valid.
All files exists.
Total Sites:  26335
========
====================

Difference between Raw and Processed

In [7]:
display_diff(batches, raws, procs, PLOT_PATH, 10,4)
batch2
========
batch3
========
batch4
========
batch5
========
In [9]:
for batch in batches:
    with contextlib.redirect_stdout(io.StringIO()):
        var = sample_and_calc_variance(root_directory_proc, batch, 
                                       sample_size_per_markers=200,
                                       cond_count=2, rep_count=len(reps), 
                                       num_markers=len(dnls_markers))
    print(f'{batch} var: ',var)
    
batch2 var:  0.0074132928873828965
batch3 var:  0.007107947532662469
batch4 var:  0.0070460634463774836
batch5 var:  0.006884715302232348

Number of sites in each batch and cell line

In [10]:
plot_sites_count(df, dnls_expected_raw, dnls_lines_order, dnls_custom_palette, split_to_reps=True)

Number of Cells in Site for each batch and cell line

In [11]:
df_no_empty_sites = df[df.n_valid_tiles !=0]
plot_cell_count(df_no_empty_sites, dnls_lines_order, dnls_custom_palette, whole_cells=True)

plot_cell_count(df_no_empty_sites, dnls_lines_order, dnls_custom_palette, whole_cells=False)

# can add norm=True to norm by max

number of valid tiles per image (site)

In [12]:
plot_n_valid_tiles_count(df, custom_palette, reps, batch_min=2, batch_max=5)

Heatmap QC per batch, panel and cell line(tiles that passed QC condition)

In [13]:
plot_hm(df, split_by='rep', rows='cell_line', columns='panel')

Assessing Staining Reproducibility and Outliers

In [ ]:
for batch in batches:
    print(batch)
    raw_df = run_calc_hist_new(f'deltaNLS_sort/{batch}',dnls_cell_lines_for_disp,
                      dnls_markers,hist_sample=10,ncols=2, nrows=2,
                               cond_count=2, rep_count=len(reps),
                             sample_size_per_markers=200)
    print("="*30)
    
batch2
In [ ]:
# save notebook as HTML ( the HTML will be saved in the same folder the original script is)
from IPython.display import display, Javascript
display(Javascript('IPython.notebook.save_checkpoint();'))
os.system('jupyter nbconvert --to html src/preprocessing/notebooks/cell_count_stats_analysis_dNLS.ipynb')
In [ ]: